package com.example.dessertstoressm.controller;

import com.example.dessertstoressm.Service.OrderService;
import com.example.dessertstoressm.domain.Account;
import com.example.dessertstoressm.domain.Cart;
import com.example.dessertstoressm.domain.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/order")
public class orderController {
    @Autowired
    private Order order;

    @Autowired
    private Account account;

    @Autowired
    private Cart cart;


    @RequestMapping(value="/confirmOrder" , method = RequestMethod.POST)
    public String ConfirmOrder( HttpServletRequest req) {
        HttpSession session = req.getSession();
        session.setAttribute("page", "confirmOrder");
        String shippingAddressRequired = req.getParameter("shippingAddressRequired");
        order = (Order)session.getAttribute("order");
        account = (Account)session.getAttribute("account");
        cart = (Cart) session.getAttribute("cart");

        if (shippingAddressRequired == null){
            return "order/confirmOrder";
        }else{
            String shipToFirstName = req.getParameter("shipToFirstName");
            String shipToLastName = req.getParameter("shipToLastName");
            String shipAddress1 = req.getParameter("shipAddress1");
            String shipAddress2 = req.getParameter("shipAddress2");
            String shipCity = req.getParameter("shipCity");
            String shipState = req.getParameter("shipState");
            String shipZip = req.getParameter("shipZip");
            String shipCountry = req.getParameter("shipCountry");

            order.setShipToFirstName(shipToFirstName);
            order.setShipToLastName(shipToLastName);
            order.setShipAddress1(shipAddress1);
            order.setShipAddress2(shipAddress2);
            order.setShipCity(shipCity);
            order.setShipState(shipState);
            order.setShipZip(shipZip);
            order.setCourier(shipCountry);
            session.setAttribute("order", order);

            account = (Account) session.getAttribute("account");

            return "order/confirmOrder";
        }

    }

//    @RequestMapping(value = "/shippingAddress", method = RequestMethod.POST)
//    public String ShippingAddress(HttpServletRequest req){
//        String shipToFirstName = req.getParameter("shipToFirstName");
//        String shipToLastName = req.getParameter("shipToLastName");
//        String shipAddress1 = req.getParameter("shipAddress1");
//        String shipAddress2 = req.getParameter("shipAddress2");
//        String shipCity = req.getParameter("shipCity");
//        String shipState = req.getParameter("shipState");
//        String shipZip = req.getParameter("shipZip");
//        String shipCountry = req.getParameter("shipCountry");
//
//        HttpSession session = req.getSession();
//        order = (Order)session.getAttribute("order");
//
//        order.setShipToFirstName(shipToFirstName);
//        order.setShipToLastName(shipToLastName);
//        order.setShipAddress1(shipAddress1);
//        order.setShipAddress2(shipAddress2);
//        order.setShipCity(shipCity);
//        order.setShipState(shipState);
//        order.setShipZip(shipZip);
//        order.setCourier(shipCountry);
//
//        session.setAttribute("order", order);
//
//        account = (Account) session.getAttribute("account");
//
//        return "order/confirmOrder";
//
//    }

//    @RequestMapping("/shippingForm")
//    public String ShippingForm( HttpServletRequest req){
//        HttpSession session = req.getSession();
//        account = (Account)session.getAttribute("account");
//
//        if(account == null){
//            return "account/signon";
//        }else{
//            return "order/shippingForm";
//        }
//    }

    @Autowired
    private List<Order> orderList;

    @Autowired
    private OrderService orderService;

    @RequestMapping("/listOrders")
    public String ListOrders(@RequestParam("username") String username , HttpServletRequest req){
        if(username == null){

        }

        orderList = orderService.getOrdersByUsername(username);

        HttpSession session = req.getSession();
        session.setAttribute("orderList" , orderList);

        System.out.println(orderList);
        session.setAttribute("page", "listOrders");
        return "order/listOrders";
    }

    @RequestMapping("/newOrder")
    public String NewOrder(HttpServletRequest req){
        HttpSession session = req.getSession();
        account = (Account) session.getAttribute("account");
        cart = (Cart) session.getAttribute("cart");

        if(account == null){
            session.setAttribute("message", "You must sign on before attempting to check out.  Please sign on and try checking out again.");
            return "account/signon";
        }else if(cart !=null) {
            order = new Order();
            order.initOrder(account, cart);
            session.setAttribute("order", order);

            return "order/newOrder";
        }else{
            session.setAttribute("message", "An order could not be created because a cart could not be found.");

            return "common/error";
        }

    }

    @RequestMapping("/viewOrder")
    public String ViewOrder(HttpServletRequest req){
        HttpSession session = req.getSession();
        String page = (String) session.getAttribute("page");
        account = (Account) session.getAttribute("account");

        if(Objects.equals(page, "listOrders"))     //如果请求是从查看订单页面发出的
        {
            String orderIdStr = req.getParameter("orderId");
            int orderId = Integer.parseInt(orderIdStr);
            System.out.println(orderId);

            order = orderService.getOrder(orderId);
            System.out.println(order.getShipZip());
            session.setAttribute("order", order);

            if(order != null) {
                return "order/viewOrder";
            }else {
                session.setAttribute("message",  "You may only view your own orders.");

                return "common/error";
            }


        }else {     //如果请求是从confirmOrder界面发出的
            System.out.println("confirm");


            order = (Order) session.getAttribute("order");
            cart = (Cart) session.getAttribute("cart");

            if(order != null){

                //订单插入数据库
                System.out.println("begin to insert order");

                orderService.insertOrder(order);
                session.setAttribute("order", order);

                //清空购物车
                cart = null;
                session.setAttribute("cart", cart);
                System.out.println("cart is empty");

                session.setAttribute("message", "Thank you, your order has been submitted.");

                return "order/viewOrder";
            }else {
                session.setAttribute("message",  "You may only view your own orders.");

                return "common/error";
            }
        }

    }
}
